package com.jxdinfo.hussar.cloud.module.example.dljc.basketnotice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jxdinfo.hussar.cloud.module.example.dljc.basketnotice.dao.BasketNoticeMapper;
import com.jxdinfo.hussar.cloud.module.example.dljc.basketnotice.model.BasketNotice;
import com.jxdinfo.hussar.cloud.module.example.dljc.basketnotice.service.BasketNoticeService;
import com.jxdinfo.hussar.cloud.module.example.dljc.basketnotice.vo.BasketNoticePageVO;
import com.jxdinfo.hussar.cloud.module.example.dljc.basketnotice.vo.BasketNoticeSearchVO;
import com.jxdinfo.hussar.cloud.module.example.dljc.basketnotice.vo.BasketNoticeVO;
import com.jxdinfo.hussar.cloud.module.example.tjjc.util.DateUtil;
import com.jxdinfo.hussar.platform.core.base.apiresult.ApiResponse;
import com.jxdinfo.hussar.platform.core.utils.BeanUtil;
import com.jxdinfo.hussar.support.datasource.annotations.HussarDs;
import com.jxdinfo.hussar.support.mp.base.service.impl.HussarServiceImpl;
import org.apache.commons.lang3.ObjectUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 吊篮异常通知服务实现类
 */
@Service("dljc.basketnotice.BasketNoticeServiceImpl")
@HussarDs("master")
public class BasketNoticeServiceImpl extends HussarServiceImpl<BasketNoticeMapper, BasketNotice> implements BasketNoticeService {

    private static final Logger logger = LoggerFactory.getLogger(BasketNoticeServiceImpl.class);

    private static final String RETURN_CODE = "0";

    @Autowired
    private BasketNoticeMapper basketNoticeMapper;

    @Override
    public ApiResponse<BasketNoticePageVO> findByPage(BasketNoticeSearchVO basketNoticeSearchVO) {
        QueryWrapper<BasketNotice> wrapper = new QueryWrapper<>();

        Page<BasketNotice> page = new Page<>();
        page.setPages(basketNoticeSearchVO.getPage());
        page.setSize(basketNoticeSearchVO.getRows());

        if(ObjectUtils.isNotEmpty(basketNoticeSearchVO.getPointId())) {
            wrapper.eq("point_id", basketNoticeSearchVO.getPointId());
        }
        if(ObjectUtils.isNotEmpty(basketNoticeSearchVO.getNoticeType())) {
            wrapper.eq("notice_type", basketNoticeSearchVO.getNoticeType());
        }
        if(ObjectUtils.isNotEmpty(basketNoticeSearchVO.getNoticeLevel())) {
            wrapper.eq("notice_level", basketNoticeSearchVO.getNoticeLevel());
        }
        if(ObjectUtils.isNotEmpty(basketNoticeSearchVO.getTyProjectCode())) {
            wrapper.eq("ty_project_code", basketNoticeSearchVO.getTyProjectCode());
        }
        if(ObjectUtils.isNotEmpty(basketNoticeSearchVO.getTyCreateTimeStart())) {
            wrapper.ge("send_time", basketNoticeSearchVO.getTyCreateTimeStart());
        }
        if(ObjectUtils.isNotEmpty(basketNoticeSearchVO.getTyCreateTimeEnd())) {
            wrapper.le("send_time", basketNoticeSearchVO.getTyCreateTimeEnd());
        }
        wrapper.eq("notice_status",1);
        wrapper.orderByDesc("send_time");

        Page<BasketNotice> basketNoticePage = basketNoticeMapper.selectPage(page, wrapper);
        BasketNoticePageVO basketNoticePageVO = new BasketNoticePageVO();
        basketNoticePageVO.setCode(RETURN_CODE);
        basketNoticePageVO.setCount(basketNoticePage.getTotal());
        basketNoticePageVO.setData(basketNoticePage.getRecords());

        return ApiResponse.success(basketNoticePageVO);
    }

    @Override
    public ApiResponse<List<BasketNoticeVO>> findByList(BasketNoticeSearchVO basketNoticeSearchVO) {
        QueryWrapper<BasketNotice> wrapper = new QueryWrapper<>();

        if(ObjectUtils.isNotEmpty(basketNoticeSearchVO.getPointId())) {
            wrapper.eq("point_id", basketNoticeSearchVO.getPointId());
        }
        if(ObjectUtils.isNotEmpty(basketNoticeSearchVO.getNoticeType())) {
            wrapper.eq("notice_type", basketNoticeSearchVO.getNoticeType());
        }
        if(ObjectUtils.isNotEmpty(basketNoticeSearchVO.getNoticeLevel())) {
            wrapper.eq("notice_level", basketNoticeSearchVO.getNoticeLevel());
        }
        if(ObjectUtils.isNotEmpty(basketNoticeSearchVO.getTyProjectCode())) {
            wrapper.eq("ty_project_code", basketNoticeSearchVO.getTyProjectCode());
        }
        if(ObjectUtils.isNotEmpty(basketNoticeSearchVO.getTyCreateTimeStart())) {
            wrapper.ge("send_time", basketNoticeSearchVO.getTyCreateTimeStart());
        }
        if(ObjectUtils.isNotEmpty(basketNoticeSearchVO.getTyCreateTimeEnd())) {
            wrapper.le("send_time", basketNoticeSearchVO.getTyCreateTimeEnd());
        }
        wrapper.eq("notice_status",1);
        wrapper.orderByDesc("send_time");

        List<BasketNotice> basketNotices = basketNoticeMapper.selectList(wrapper);
        List<BasketNoticeVO> basketNoticeVOS = BeanUtil.copyToList(basketNotices, BasketNoticeVO.class);

        return ApiResponse.success(basketNoticeVOS);
    }

    @Override
    public ApiResponse<String> add(BasketNoticeSearchVO basketNoticeSearchVO) {
        BasketNotice basketNotice = new BasketNotice();
        BeanUtil.copyProperties(basketNoticeSearchVO, basketNotice);
        basketNotice.setTyProjectCode("10101");//todo
        basketNotice.setTyCreateTime(DateUtil.toLocalDateTime(new Date()));
        basketNotice.setTyCreateUserid("ContextUtil.getUserId()");//todo
        basketNotice.setTyCreateUserName("ContextUtil.getUserName()");//todo

        basketNoticeMapper.insert(basketNotice);
        return ApiResponse.success();
    }

    @Override
    public ApiResponse<String> updateTowerNotice(BasketNoticeSearchVO basketNoticeSearchVO) {
        BasketNotice basketNotice = basketNoticeMapper.selectById(basketNoticeSearchVO.getId());

        if(basketNotice != null){

            BeanUtil.copyProperties(basketNoticeSearchVO, basketNotice);
            basketNotice.setTyLastUpdateTime(DateUtil.toLocalDateTime(new Date()));
            basketNotice.setTyLastUpdateUserid("ContextUtil.getUserId()");//todo
            basketNotice.setTyLastUpdateUserName("ContextUtil.getUserName()");//todo

            basketNoticeMapper.updateById(basketNotice);
        }

        return ApiResponse.success();
    }

    @Override
    public ApiResponse<String> deleteById(BasketNoticeSearchVO basketNoticeSearchVO) {
        BasketNotice basketNotice = basketNoticeMapper.selectById(basketNoticeSearchVO.getId());

        if(basketNotice != null){

            basketNoticeMapper.deleteById(basketNotice.getId());
        }

        return ApiResponse.success();
    }
}
